package com.gxh.controller.admin;


import com.gxh.dto.LocationDTO;
import com.gxh.dto.LocationPageQueryDTO;
import com.gxh.entity.Location;
import com.gxh.result.PageResult;
import com.gxh.result.Result;
import com.gxh.service.LocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/location")
@Slf4j
@Api(tags = "位置管理")
public class LocationController {

    @Autowired
    private LocationService locationService;

    /**
     * 位置分页查询
     * @param locationPageQueryDTO
     * @return
     */
    @GetMapping ("/page")
    @ApiOperation(value = "位置分页查询")
    public Result<PageResult> page(LocationPageQueryDTO locationPageQueryDTO) {
        log.info("位置分页查询:{}", locationPageQueryDTO);
        PageResult pageResult = locationService.pageQuery(locationPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增位置
     * @param locationDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增位置")
    @CacheEvict(value = "locationCache",key = "'list'")
    public Result save(@RequestBody LocationDTO locationDTO) {
        log.info("新增位置:{}", locationDTO);
        locationService.save(locationDTO);
        return Result.success();
    }

    /**
     * 修改位置
     * @param locationDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改位置")
    @CacheEvict(value = "locationCache",key = "'list'")
    public Result update(@RequestBody LocationDTO locationDTO) {
        log.info("修改位置:{}", locationDTO);
        locationService.update(locationDTO);
        return Result.success();
    }

    /**
     * 删除位置
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除位置")
    @CacheEvict(value = "locationCache",key = "'list'")
    public Result delete(@RequestBody List<Long> ids) {
        log.info("批量删除位置:{}", ids);
        locationService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询位置
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询位置")
    public Result<Location> getById(@PathVariable Long id) {
        log.info("根据id查询位置:{}", id);
        Location location = locationService.getById(id);
        return Result.success(location);
    }

    /**
     * 查询所有位置
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有位置")
    public Result<List<Location>> list() {
        log.info("查询所有位置");
        List<Location> list = locationService.list();
        return Result.success(list);
    }
}
